'use strict';
var gulp = require('gulp'),
  less = require('gulp-less'),  //less编译
  plumber = require('gulp-plumber'), //less错误，让watch更新继续
  minify = require('gulp-minify-css'), //css压缩
  htmlmini = require('gulp-minify-html'), //html压缩  
  concat = require('gulp-concat'), //文件合并
  uglify = require('gulp-uglify'), //js压缩
  rename = require('gulp-rename'), //文件重命名
  rev = require('gulp-rev'), //更改版本号
  revCollector = require('gulp-rev-collector'), //gulp-rev的插件，用于html模板更改引用路径
  imagemin = require('gulp-imagemin'), //压缩图片
  clean = require('gulp-clean'), //清空文件夹
  combiner = require('stream-combiner2'), //streams错误处理
  babel = require('gulp-babel'),
  stripDebug = require('gulp-strip-debug'), //去掉console.log信息
  proxy = require('http-proxy-middleware'),
  browserSync = require('browser-sync').create(), //浏览器同步刷新
  runSequence = require('run-sequence');  //同步执行任务
var reload = browserSync.reload;  // 使用变量引用 `reload` 方法

const projectDir = require('./config/projectDir');  //项目路径
const proxyConfig = require('./config/proxyConfig');
const holderProxy = proxy('/api', proxyConfig.settings)

//编译less
gulp.task('less', function () {
  return gulp.src(projectDir.cssDir+'/*.less')
    .pipe(plumber())
    .pipe(less())
    .pipe(gulp.dest(projectDir.cssDir))
    .pipe(reload({stream:true}));
});

//压缩css
gulp.task('minifyCss', function () {
  return gulp.src([projectDir.cssDir+'/**/*.css', '!'+projectDir.cssDir+'/**/*.min.css'])
    // .pipe(rename({
    //   suffix: '.min'
    // }))
    .pipe(minify())
    .pipe(rev())
    .pipe(gulp.dest('dist/app/css'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('dist/rev/css'))
    .pipe(reload({stream:true}));
});

//压缩js
gulp.task('minifyJs', function () {
   var combined = combiner.obj([
     gulp.src(projectDir.jsDir+'/*.js'),
     babel(),
     uglify(), 
     stripDebug(), //删除console
     rev(),
     gulp.dest('dist/app/js'),
     rev.manifest(),
     gulp.dest('dist/rev/js'),
     reload({stream:true})
   ]);
    // 任何在上面的 stream 中发生的错误，都不会抛出，
    // 而是会被监听器捕获
    combined.on('error', console.error.bind(console));
    return combined;
});

gulp.task('minifyPageJs', function () {
   var combined = combiner.obj([
     gulp.src(projectDir.jsDir+'/pagejs/*.js'),
     babel(),
     uglify(), 
     stripDebug(), //删除console
     rev(),
     gulp.dest('dist/app/js/pagejs'),
     rev.manifest(),
     gulp.dest('dist/rev/js/pagejs'),
     reload({stream:true})
   ]);
    // 任何在上面的 stream 中发生的错误，都不会抛出，
    // 而是会被监听器捕获
    combined.on('error', console.error.bind(console));
    return combined;
});

//压缩图片
gulp.task('minifyImg', function () {
  return gulp.src(projectDir.imageDir+'/*')
    .pipe(imagemin())
    .pipe(gulp.dest('dist/images'));
});

//压缩html
gulp.task('htmlmini', function () {
   return gulp.src(projectDir.htmlDir+'*.html')
      .pipe(htmlmini())
      .pipe(gulp.dest('dist/app/'))
      .pipe(reload({stream:true}));
})

//通过hash来精确定位到html模板中需要更改的部分,然后将修改成功的文件生成到指定目录
gulp.task('rev',function(){
    return gulp.src(['dist/rev/**/*.json','src/*.html'])
    .pipe(revCollector())
    .pipe(htmlmini()) //压缩html
    .pipe(gulp.dest('dist/app/'));
});

//清理文件
gulp.task('clean', function () {
  return gulp.src('dist/',{read:false})
    .pipe(clean())
});  

//复制压缩版css
gulp.task('copyCss', function () {
  return gulp.src([projectDir.cssDir+'/**/*.min.css'])
    .pipe(gulp.dest('dist/app/css'))
});

//复制js插件
gulp.task('copyPlugins', function () {
  return gulp.src(projectDir.jsDir+'/plugins/**')
    .pipe(gulp.dest('dist/app/js/plugins'));
});

//复制图片文件
gulp.task('copyImage', function () {
  return gulp.src(projectDir.imageDir+'/**')
    .pipe(gulp.dest('dist/app/images'));
});

//复制文件
gulp.task('copy',['copyPlugins','copyCss','copyImage']);

//服务器代理
gulp.task('webserver', function () {
  browserSync.init({
    server: {
      baseDir: projectDir.baseDir
    },
    middleware: [holderProxy]
  });

  //监听目录下的文件，若文件发生变化，则调用任务。
  gulp.watch(projectDir.cssDir + '/**/*.less').on('change', reload);
  gulp.watch(projectDir.cssDir + '/**/*.css').on('change', reload);
  gulp.watch(projectDir.jsDir + '/**/*.js').on('change', reload);
  gulp.watch(projectDir.htmlDir + "/**/*.html").on('change', reload);
});

//启动开发
gulp.task('dev', ['webserver'], function () {
  
});

//打包
gulp.task('build',function(callback) {
    runSequence('clean',['copy','minifyCss','minifyJs','minifyPageJs'],'rev')
});